﻿using ER.Dynamic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Entity;
using System.Linq;
using System.Reflection;
using System.Web;

namespace ER.Tests.Data
{
    public static class RuntimeDbContextHelper
    {
        //TODO convert to use lamda expression
        public static void AddDisplayAttribute(this PropertyEmiter propEmiter, string displayName)
        {
            Type displayType = typeof(System.ComponentModel.DataAnnotations.DisplayAttribute);
            String[] attributePropNames = new[] { "Name" };
            String[] attributePropValues = new []{displayName};
            propEmiter.AddCustomAttribute(displayType, attributePropNames, attributePropValues);
        }

        public static dynamic Set(this DbContext db, string propertyName)
        {
            Type dbType = ((object)db).GetType();
            PropertyInfo propInfo = dbType.GetProperty(propertyName);
            dynamic result = (dynamic)propInfo.GetValue(db, null);
            return result;
        }
        public static IList ToList(this object db)
        {
            IEnumerable iEnumerable = (IEnumerable)db;
            IList result = new List<object>();
            foreach (var item in iEnumerable)
            {
                result.Add(item);
            }
            return result;
        }
    }
}